<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class StockDB extends BaseDB
{
    /**
     * 库
     * fromType 来源平台：1快手，2抖音，3刷宝，4快报，默认0
     * fileTitle 文件名
     * fileLink 文件链接
     * del 0未删除，1已删除
     */

    /**
     * 库历史
     * uid 用户UID
     * vid 视频ID
     * del 0未删除，1已删除
     */

    protected $table = 'stock';
    protected $tHistory = 'stock_history'; //浏览历史
    protected $tZan = 'stock_zan'; //点赞
    protected $tCollect = 'stock_collect'; //收藏
    protected $tPlus = 'stock_plus'; //创作

    public function __construct()
    {
    }

    /**
     * 来源平台
     */
    public function getTypes()
    {
        return array(
            1 => "快手", "抖音", "刷宝", "快报", "", "",
        );
    }

    /**
     * 来源平台
     */
    public function getTypeName($type)
    {
        $types = $this->getTypes();
        return array_key_exists($type, $types) ? $types[$type] : '';
    }

    //后台列表
    public function getAdminIndex($page,$size)
    {
        $pageStart = $page * $size;
        $query = DB::table($this->table)
//            ->where('del',0)
            ->orderBy('id','asc');
        $queryTotal = $query->count();
        $models = $query->skip($pageStart)
            ->take($size)
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $models = $this->jsonToArr($models);
        $dataArr = array();
        foreach ($models as $model) {
            $fileTitle = $model['fileTitle'] ? $model['fileTitle'] : '自定义';
            $fileType = $model['fromType'] ? $model['fromType'] : 0;
            $fileTypeName = $this->getTypeName($model['fromType']);
            $fileImg = $model['fileImg'] ? $model['fileImg'] : '';
            $fileLink = $model['fileLink'] ? $model['fileLink'] : '';
            $originLink = $model['originLink'] ? $model['originLink'] : '';
            $ratio = 1280 / 720;
            //点赞量
            $zanTotal = DB::table($this->tZan)->where('del',0)
                ->where('vid',$model['id'])->count();
            //收藏量
            $collectTotal = DB::table($this->tCollect)->where('del',0)
                ->where('vid',$model['id'])->count();
            $dataArr[] = [
                'videoId' => $model['id'],
                'videoTitle' => $fileTitle,
                'videoType' => $fileType,
                'videoTypeName' => $fileTypeName,
                'videoImg' => $fileImg,
                'videoRatio' => $ratio,
                'videoLink' => $fileLink,
                'originLink' => $originLink,
                'zanTotal' => $zanTotal,
                'collectTotal' => $collectTotal,
                'del' => $model['del'],
            ];
        }
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$dataArr, 'total'=>$queryTotal);
    }

    //后台单个
    public function getAdminOne($vid)
    {
        $models = DB::table($this->table)
            ->where('del',0)
            ->where('id',$vid)
            ->orderBy('id','asc')
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $model = $this->jsonToArr($models[0]);
        $fileTitle = $model['fileTitle'] ? $model['fileTitle'] : '自定义';
        $fileType = $model['fromType'] ? $model['fromType'] : 0;
        $fileTypeName = $this->getTypeName($model['fromType']);
        $fileImg = $model['fileImg'] ? $model['fileImg'] : '';
        $fileLink = $model['fileLink'] ? $model['fileLink'] : '';
        $ratio = 1280 / 720;
        $dataArr = [
            'videoId' => $model['id'],
            'videoTitle' => $fileTitle,
            'videoType' => $fileType,
            'videoTypeName' => $fileTypeName,
            'videoImg' => $fileImg,
            'videoRatio' => $ratio,
            'videoLink' => $fileLink,
        ];
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$dataArr);
    }

    //后台单个
    public function getAdminOneByLink($vlink)
    {
        $models = DB::table($this->table)
            ->where('del',0)
            ->where('fileLink',$vlink)
            ->orderBy('id','asc')
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $model = $this->jsonToArr($models[0]);
        $fileTitle = $model['fileTitle'] ? $model['fileTitle'] : '自定义';
        $fileType = $model['fromType'] ? $model['fromType'] : 0;
        $fileTypeName = $this->getTypeName($model['fromType']);
        $fileImg = $model['fileImg'] ? $model['fileImg'] : '';
        $fileLink = $model['fileLink'] ? $model['fileLink'] : '';
        $ratio = 1280 / 720;
        $dataArr = [
            'videoId' => $model['id'],
            'videoTitle' => $fileTitle,
            'videoType' => $fileType,
            'videoTypeName' => $fileTypeName,
            'videoImg' => $fileImg,
            'videoRatio' => $ratio,
            'videoLink' => $fileLink,
        ];
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$dataArr);
    }

    /**
     * 新增表单
     */
    public function setAdd($items)
    {
        if (!$items) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        //开启事务
        DB::beginTransaction();
        try {
            DB::table($this->table)->insertGetId($items);
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 新增表单
     */
    public function setUpdate($id, $items)
    {
        if (!$id || !$items) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        //开启事务
        DB::beginTransaction();
        try {
            DB::table($this->table)->where('id',$id)->update($items);
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 后台删除单个
     */
    public function setAdminDel($vid, $del)
    {
        if (!$vid || !in_array($del, [0,1])) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        //开启事务
        DB::beginTransaction();
        try {
            DB::table($this->table)->where('id',$vid)
                ->update([
                    'del' => $del,
                    'updated_at' => time(),
                ]);
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * APP列表
     */
    public function getAppIndex($page,$size,$uid)
    {
        $query = DB::table($this->tHistory)->where('del',0)
            ->where('uid',$uid);
        $queryTotal = $query->count();
        $models = $query->orderBy('id','asc')
            ->skip($page)->take($size)
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $models = $this->jsonToArr($models);
        $dataArr = array();
        foreach ($models as $model) {
            $resVideos = DB::table($this->table)->where('id',$model['vid'])->get();
            return $resVideos;
            $videoOne = count($resVideos) ? $this->jsonToArr($resVideos[0]) : '';
            $vid = ($videoOne&&$videoOne['id']) ? $videoOne['id'] : 0;
            $fileTitle = ($videoOne&&$videoOne['fileTitle']) ? $videoOne['fileTitle'] : '自定义';
            $fileType = ($videoOne&&$videoOne['fromType']) ? $videoOne['fromType'] : 0;
            $fileImg = ($videoOne&&$videoOne['fileImg']) ? $videoOne['fileImg'] : '';
            $fileLink = ($videoOne&&$videoOne['fileLink']) ? $videoOne['fileLink'] : '';
            $ratio = 1280 / 720;
            //点赞、收藏、创作统计
            $queryZan = DB::table($this->tZan)
                ->where('del',0)
                ->where('vid',$vid);
            $zanTotal = $queryZan->count();
            $queryCollect = DB::table($this->tCollect)->where('del',0)
                ->where('vid',$vid);
            $collectTotal = $queryCollect->count();
            $queryPlus = DB::table($this->tPlus)->where('del',0)
                ->where('vid',$vid);
            $plusTotal = $queryPlus->count();
            if ($uid) {
                $zanUser = $queryZan->where('uid',$uid)->count();
                $collectUser = $queryCollect->where('uid',$uid)->count();
                $plusUser = $queryPlus->where('uid',$uid)->count();
            } else {
                $zanUser = 0;
                $collectUser = 0;
                $plusUser = 0;
            }
            $dataArr[] = [
                'videoId' => $vid,
                'videoTitle' => $fileTitle,
                'videoType' => $fileType,
                'videoImg' => $fileImg,
                'videoRatio' => $ratio,
                'videoLink' => $fileLink,
                //点赞总数
                'zanTotal' => $zanTotal,
                //当前用户是否点赞
                'zanUser' => $zanUser,
                //收藏总数
                'collectTotal' => $collectTotal,
                //当前用户是否收藏
                'collectUser' => $collectUser,
                //加入创作总数
                'plusTotal' => $plusTotal,
                //当前视频是否加入创作
                'plusUser' => $plusUser,
            ];
        }
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$dataArr, 'total'=>$queryTotal);
    }

    /**
     * APP单个
     */
    public function getAppOne($uid,$ids)
    {
        $model = DB::table($this->table)->where('del',0)
            ->whereNotIn('id',$ids)
            ->orderBy('id','asc')
            ->first();
        if (!$model) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $fileTitle = $model->fileTitle ? $model->fileTitle : '自定义';
        $fileType = $model->fromType ? $model->fromType : 0;
        $fileImg = $model->fileImg ? $model->fileImg : '';
        $fileLink = $model->fileLink ? $model->fileLink : '';
        $ratio = 1280 / 720;
        //点赞、收藏、创作统计
        $queryZan = DB::table($this->tZan)
            ->where('del',0)
            ->where('vid',$model->id);
        $zanTotal = $queryZan->count();
        $queryCollect = DB::table($this->tCollect)->where('del',0)
            ->where('vid',$model->id);
        $collectTotal = $queryCollect->count();
        $queryPlus = DB::table($this->tPlus)->where('del',0)
            ->where('vid',$model->id);
        $plusTotal = $queryPlus->count();
        if ($uid) {
            $zanUser = $queryZan->where('uid',$uid)->count();
            $collectUser = $queryCollect->where('uid',$uid)->count();
            $plusUser = $queryPlus->where('uid',$uid)->count();
        } else {
            $zanUser = 0;
            $collectUser = 0;
            $plusUser = 0;
        }
        $items = [
            'videoId' => $model->id,
            'videoTitle' => $fileTitle,
            'videoType' => $fileType,
            'videoImg' => $fileImg,
            'videoRatio' => $ratio,
            'videoLink' => $fileLink,
            //点赞总数
            'zanTotal' => $zanTotal,
            //当前用户是否点赞
            'zanUser' => $zanUser,
            //收藏总数
            'collectTotal' => $collectTotal,
            //当前用户是否收藏
            'collectUser' => $collectUser,
            //加入创作总数
            'plusTotal' => $plusTotal,
            //当前用户是否收藏
            'plusUser' => $plusUser,
        ];
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$items);
    }

    /**
     * 扩增历史
     * @param $uid 用户ID
     * @param $vid 库视频ID
     */
    public function setHistory($uid,$vid)
    {
        if (!$uid || !$vid) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        //开启事务
        DB::beginTransaction();
        try {
            $model = DB::table($this->tHistory)->where('del',0)
                ->where('uid',$uid)->where('vid',$vid)
                ->first();
            if (!$model) {
                $lastId = DB::table($this->tHistory)
                    ->insertGetId([
                        'uid' => $uid,
                        'vid' => $vid,
                        'created_at' => time(),
                    ]);
            } else {
                $lastId = $model->id;
            }
            $dataArr = array(
                'lastId' => $lastId,
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 删除历史
     * @param $uid 用户ID
     * @param $vids 首页视频ID集合
     */
    public function setHistoryDel($uid,$vids)
    {
        if (!$uid || !$vids) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        DB::table($this->tHistory)->where('uid',$uid)
            ->whereIn('vid',$vids)->update([
                'del' => 1,
                'updated_at' => time(),
            ]);
        return array('code'=>'0', 'msg'=>'操作成功');
    }

    /**
     * 点赞获取
     * @param $uid 用户ID
     * @param $vid 首页视频ID
     */
    public function getZanOne($uid,$vid)
    {
        if (!$uid || !$vid) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        $models = DB::table($this->tZan)->where('del',0)
            ->where('uid',$uid)->where('vid',$vid)
            ->get();
        if (!count($models)) {
            return array('code'=>'-2', 'msg'=>'没有数据');
        }
        $dataArr = $this->jsonToArr($models[0]);
        return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
    }

    /**
     * 点赞增加
     * @param $uid 用户ID
     * @param $vid 首页视频ID
     */
    public function setZanAdd($uid,$vid)
    {
        if (!$uid || !$vid) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        //开启事务
        DB::beginTransaction();
        try {
            $lastId = DB::table($this->tZan)->insertGetId([
                'uid' => $uid,
                'vid' => $vid,
                'created_at' => time(),
            ]);
            $dataArr = array(
                'lastId' => $lastId,
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 点赞移除
     * @param $uid 用户ID
     * @param $vid 首页视频ID
     */
    public function setZanDel($uid,$vid)
    {
        if (!$uid || !$vid) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        //开启事务
        DB::beginTransaction();
        try {
            $lastId = DB::table($this->tZan)->where('uid',$uid)
                ->where('vid',$vid)->update([
                    'del' => 1,
                    'updated_at' => time(),
                ]);
            $dataArr = array(
                'lastId' => $lastId,
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 收藏获取
     * @param $uid 用户ID
     * @param $vid 首页视频ID
     */
    public function getCollectOne($uid,$vid)
    {
        if (!$uid || !$vid) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        $models = DB::table($this->tCollect)->where('del',0)
            ->where('uid',$uid)->where('vid',$vid)
            ->get();
        if (!count($models)) {
            return array('code'=>'-2', 'msg'=>'没有数据');
        }
        $dataArr = $this->jsonToArr($models[0]);
        return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
    }

    /**
     * 收藏增加
     * @param $uid 用户ID
     * @param $vid 首页视频ID
     */
    public function setCollectAdd($uid,$vid)
    {
        if (!$uid || !$vid) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        //开启事务
        DB::beginTransaction();
        try {
            $lastId = DB::table($this->tCollect)->insertGetId([
                'uid' => $uid,
                'vid' => $vid,
                'created_at' => time(),
            ]);
            $dataArr = array(
                'lastId' => $lastId,
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 收藏移除
     * @param $uid 用户ID
     * @param $vid 首页视频ID
     */
    public function setCollectDel($uid,$vid)
    {
        if (!$uid || !$vid) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        //开启事务
        DB::beginTransaction();
        try {
            $lastId = DB::table($this->tCollect)->where('uid',$uid)
                ->where('vid',$vid)->update([
                    'del' => 1,
                    'updated_at' => time(),
                ]);
            $dataArr = array(
                'lastId' => $lastId,
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }
}